package com.example.feifei.fragment.buy.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.feifei.fragment.buy.adapter.ShopCartAdapter;
import com.example.feifei.fragment.buy.bean.ShopingBean;
import com.example.feifei.newproject.R;
import com.example.feifei.utils.LogUtils;
import com.example.feifei.utils.ToastUtil;
import com.jude.easyrecyclerview.EasyRecyclerView;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 购物车Fragment
 * Created by feifei on 2017/12/5.
 */

public class ShoppingFragment extends Fragment implements View.OnClickListener{

    private View root_view;

    private Context mContext;

    //商品列表
    @BindView(R.id.recycle_shopList)
    EasyRecyclerView recycle_shopList;
    //已选商品按钮
    @BindView(R.id.shop_checkAll)
    CheckBox shop_checkAll;
    //编辑按钮
    @BindView(R.id.shop_edit)
    TextView shop_edit;
    //选择删除布局
    @BindView(R.id.layout_selectAll)
    RelativeLayout layout_selectAll;
    //底部结算布局
    @BindView(R.id.layout_editClearing)
    RelativeLayout layout_editClearing;
    //删除按钮
    @BindView(R.id.btn_deleteAll)
    TextView btn_deleteAll;
    //结算按钮
    @BindView(R.id.btn_shop_clearing)
    TextView btn_shop_clearing;

    /******************************************    变量适配    ********************************************/
    //记录当前页面状态是编辑还是选择删除
    private boolean isSelectState = false;
    //商品列表适配器
    private ShopCartAdapter shopCartAdapter;
    //总体商品列表数据
    private List<ShopingBean> shopingList;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        root_view = inflater.inflate(R.layout.fragment_shopping,null);
        ButterKnife.bind(this,root_view);
        return root_view;
    }

    @Override
    public void onResume() {
        super.onResume();
        initView();
        setListener();
    }

    private void initView() {

        recycle_shopList.setLayoutManager(new LinearLayoutManager(mContext));
        shopCartAdapter = new ShopCartAdapter(mContext);
        recycle_shopList.setAdapter(shopCartAdapter);
        //添加条目删除按钮
        recycle_shopList.setItemAnimator(new DefaultItemAnimator());
        //添加分割线
//        recycle_shopList.addItemDecoration(new DividerItemDecoration(mContext,DividerItemDecoration.VERTICAL));
        shopingList = getShopIngList();
        shopCartAdapter.addAll(shopingList);
    }


    public void setListener() {
        shop_edit.setOnClickListener(this);
        btn_deleteAll.setOnClickListener(this);
        btn_shop_clearing.setOnClickListener(this);
        //商品选择CheckBox
        shopCartAdapter.setOnSelectItemListener(new ShopCartAdapter.OnSelectItemListener() {
            @Override
            public void checkSelectClick(int position, boolean isSelect) {
                if(isSelect){
                    shopingList.get(position).setSelect(true);
                    ToastUtil.showMessage(mContext,"添加了=="+position);
                }else{
                    shopingList.get(position).setSelect(false);
                    ToastUtil.showMessage(mContext,"删除了=="+position);
                    if(shop_checkAll.isChecked()){
                        shop_checkAll.setChecked(false);
                    }
                }
            }
        });
        //全选商品按钮
        shop_checkAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(shop_checkAll.isChecked()){
                   for(int i = 0;i<shopingList.size();i++){
                       shopingList.get(i).setSelect(true);
                   }
                }
                shopCartAdapter.notifyDataSetChanged();
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //编辑按钮
            case R.id.shop_edit:
                if(isSelectState){
                    layout_selectAll.setVisibility(View.GONE);
                    layout_editClearing.setVisibility(View.VISIBLE);
                    shop_edit.setText("编辑");
                    isSelectState = false;
                }else{
                    layout_selectAll.setVisibility(View.VISIBLE);
                    layout_editClearing.setVisibility(View.GONE);
                    shop_edit.setText("完成");
                    isSelectState = true;
                }
                for(int i = 0;i<shopingList.size();i++){
                    shopingList.get(i).setSelect(false);
                }
                shopCartAdapter.notifyDataSetChanged();
                break;
            //删除按钮
            case R.id.btn_deleteAll:
                for(int i = shopingList.size()-1;i>=0;i--){
                    if(shopingList.get(i).isSelect()){
                        shopingList.remove(i);
                        shopCartAdapter.remove(i);
                    }
                }
                shopCartAdapter.notifyDataSetChanged();
                break;
            //计算按钮
            case R.id.btn_shop_clearing:
                for(int i = 0;i<shopingList.size();i++){
                    if(shopingList.get(i).isSelect()){
                        LogUtils.e("结算的商品ID=="+shopingList.get(i).getShopID());
                    }
                }
//                jumpToFragment(new ShopOrderFragment());
                break;
        }
    }

    //模拟购物车集合
    private List<ShopingBean> getShopIngList(){
        List<ShopingBean> shopList = new ArrayList<>();
        for(int i = 0;i<10;i++){
            ShopingBean shopBean = new ShopingBean();
            shopBean.setShopID(i);
            shopList.add(shopBean);
        }
        return  shopList;
    }
}
